package com.xywl.escort.view.pay.project;

import com.xywl.escort.bean.BandCardListResp;
import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.FindIsFitPayPwdResp;
import com.xywl.escort.bean.FindUserBankCardListReq;
import com.xywl.escort.bean.PayApplyResp;
import com.xywl.escort.bean.SubscribeBuyReq;
import com.xywl.escort.bean.SubscribeBuyResp;
import com.xywl.escort.bean.VerifyPayPwdReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class SubscribeOrderPayDialogPresenter implements SubscribeOrderPayDialogContract.Presenter {
    private SubscribeOrderPayDialogContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SubscribeOrderPayDialogPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(SubscribeOrderPayDialogContract.View view) {
        this.mView = view;
    }

    @Override
    public void getBandCardList(String cardType, int pageNum, int pageSize) {
        FindUserBankCardListReq req = new FindUserBankCardListReq();
        req.cardType = cardType;
        req.pageSize = pageSize;
        req.pageNum = pageNum;
        mApiServer.findUserBankCardList(req)
                .compose(RxUtil.<BandCardListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BandCardListResp>bindToLife())
                .subscribe(new BaseSubscriber<BandCardListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getBandCardListFailure();
                    }

                    @Override
                    public void onNext(BandCardListResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.getBandCardListSuccess(resp.data);
                        } else {
                            mView.getBandCardListFailure();
                        }
                    }
                });
    }

    @Override
    public void payApply(String id) {
        VerifyPayPwdReq req = new VerifyPayPwdReq();
        req.id = id;
        mApiServer.payApply(req)
                .compose(RxUtil.<PayApplyResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<PayApplyResp>bindToLife())
                .subscribe(new BaseSubscriber<PayApplyResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.payApplyFailure("支付失败");
                    }

                    @Override
                    public void onNext(PayApplyResp resp) {
                        if (null != resp) {
                            if (resp.isOk() && null != resp.data) {
                                mView.payApplySuccess(resp.data);
                            } else {
                                mView.payApplyFailure(resp.msg);
                            }
                        } else {
                            mView.payApplyFailure("支付失败");
                        }
                    }
                });
    }

    @Override
    public void isSetPayPwd(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        mApiServer.isSetPayPwd(req)
                .compose(RxUtil.<FindIsFitPayPwdResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<FindIsFitPayPwdResp>bindToLife())
                .subscribe(new BaseSubscriber<FindIsFitPayPwdResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.findIsFitPayPwdFailure("验证支付密码失败");
                    }

                    @Override
                    public void onNext(FindIsFitPayPwdResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                if (null != resp.data) {
                                    if (resp.data.status == 1) {
                                        mView.hasSetPayPwd();
                                    } else if (resp.data.status == 0) {
                                        mView.noSetPayPwd();
                                    } else {
                                        mView.findIsFitPayPwdFailure(resp.msg);
                                    }
                                } else {
                                    mView.findIsFitPayPwdFailure(resp.msg);
                                }
                            } else {
                                mView.findIsFitPayPwdFailure(resp.msg);
                            }
                        } else {
                            mView.findIsFitPayPwdFailure("验证支付密码失败");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("SubscribeOrderPayDialogPresenter").e("unSubscribe");
        mView = null;
    }
}
